Bug fix and small code cleanup