Implement Moore's algorithm, but it suffers from some bugs...