Added documentation and better error reporting