Completed implementation of John's algorithm.