Some quick bug fixes and small cleanup.

See commits for details.