refactored backend code