Added error handling, return correct response code