some refactoring to group common logic together