Fixed error handling. Previously errors were set before redirecting the user to HTTP_REFERER.