Refactored based on Daniel's saggestion