some refactoring to separate logic