Refactor move function