Various bug fixes and speed tweaks