Bug fixes and assorted improvements