Made better algorithm