bugfixes and code cleanup