Refactoring code to repository pattern