Correction to debug output
Performance improvements and bugfixes