update algorithm for detect doc for cleanup
exit on exception