Minor bug fix and refactoring.