Massive refactoring for readability