Refactor code on API side