Move sending json response into separate function