Improved document reverting and fixed crash while reverting with a running script