Massive code refactoring to enhance readability