Refactor repository code out of service