Add PHPDoc comments to various functions. Add further error checking.