Bug fixes, test fixes, and some cleanup