code cleanup + minor bugfixes