refactor to merge duplicate code,