Refactoring and cleaning up a couple of small bugs.