Refactored the move function