fixed errors and refactored code