Added ability to require authentication and better error handling