Refactored to make the code generic and to use for any repository