Refactored code to allow for better performance and looser coupling