Refactored based on feedback from Brett.