Bug fixes and cleanups