Modified Anchor algorithm in Java