code cleanup, bugfixes needed