add comments and clean up code