Bug fix and minor cleanup