Various bug-fixes identified by code examination

test code and more formal bug tracking is coming soon, I promise...