Lots of fixes and speed improvements