refactor to avoid duplicate code