Refactoring, some fixes.