deduplicate some code in motion search by pushing it into its own function and inlining it