code cleanup, bug fixes and a few additions