Bugfixes, cleanup and formatting updates