code cleanup, comments and bugfixes