first version of complete algorithm