Extracted lots of code from migrate_this_shit and added tests for this