Refactored the migration code