Refactoring and bug fixes.