Move and separate response handling out of request function