major refactoring of motion code