Complete rewrite of autoretreat. This should be faster. Let me know if there are any problems.