code cleanup and minor bugfixes