Many bug fixes and speedup