fixed some bugs, cleaned up some code