performance optimizations and bugfixes