Added documentation and some error checking.