Refactoring, fixes