Basic implementation of algorithm