Made scratch detection less I/O intensive and more robust.