Refactored based on Brandon's suggestions