Some speedups and bugfixes