Changed the head movement and fixed a memory leak