Refactored based on Dan and Zyed's comments