Nearly complete algorithm