I'll try to compile this when I get home from work, but looking at the code, I must give a simple suggestion: You should either check for collision right after moving in every single direction, or check for collision once and then retract in all directions.