Added error handling functionality