refactored code in backend to be generic