initial stab at algorithm - not working yet fully though