memory leak fix and movement flipping