fixed errors and refactored