Added input to set API key. Improved error handling.