Added some documentation and error checking