Got baseline algorithm working