Bug fixes and refactorings.
Bug fixes and refactorings.