Trying to get this algorithm to merge seemlessly with matlab...