some bugfixes and optimizations