Minor bug fixes.

Some bug fixes, still not quite finished.