possibly working algorithm