MInor changes and bugfixes, some code cleanup