refractor duplicate code