A bunch of misc bug fixes, cleanup