Added previous algorithm code