fixing code style and refactoring some code