A number of fixes and cleanup

Er, I haven't landed that PR yet :)