refactor duplicate code