Adds comments and refactors the code