some optimizations and minor bugfixes