Fix bug algorithm