bugfixes and tests for better performance