Building the algorithm