code cleanups and bug fixes