Main refactored.