Add comments and clean up some of the code