Added some error handling and comments.