Refactor move algorithm