bugfixes and optimizations