Some fixes and code cleanups