various optimizations, bugfixes