Generic threatenedSquares function improved