Improved code organization and comments