refactoring, some fixes