clean up code, add some comments