refactor code so itis more DRY