Implemented first algorithm based on Thistlethwaite's.