Cleaning up some bugs and code