refactored and working