Fix migration script during testing, some restructuring and tests.