Bug fixes and test tweaks