Various speed improvements and bug fixes