Simple implementation of the algorithm