Bug fix and code refactoring.