add comments and cleanup the code