improve error handling and redirection