fixed algorithm crashing on second run