Added better error handling and proper docs.