Massive code refactoring