Refactored based on Milo's comments