implemented more robust error handling