implement cancel operations
handle end of document more reliably