Added contracts
Made cycle-length amortized O(1) and added a O(m+n) comparison.