Added error checking for empty documents