Bug fix and clean up