Misc cleanup and bug fixes