various cleanup and bug fixes