tried minor refactoring, not tested yet