solved problems and refactored code