Fixed part of bug introduced in previous commit.

Many fixes/cleanups.