refactor code for less duplication