Refactored some code to provide a better base for future extension and maintainability.