optimizations and refactoring in preparation for better object oriented design