Q4 code mostly works. Slight heuristic modification still needed for R subset