Refactoring and small fixes.