code cleanup, bugfixes