working algorithm (no visualisation)