Several fixes and cleanups