Got something that looks like it's working. Still missing the extension for finding longest repeated substring though.