add some comments and clean up code