comments and refactor the code a little bit