Added some documentation and some additional error handling.