A bunch of bug fixes and tweaks