Some code clean up and add some comments