Implemented Daniel's algorithm.