figured out that algorithm