Algorithm works correctly again