Code cleanups, bug fixes