refactored and improved gem