migrated and semi refactored