Code refactoring and bug fixes.