adding documentation and better error checking