Refactor/move logic to backend