removed comments and refactored some logic