Fix error handling and redirect