Added error handling code, and additional documentation.