refactored and complete