Refactored backend code to be more DRY