fixing code and refactoring