Status codes were not being being set correctly because the response was sent before the code was set.