Fix some bugs, cleanup