Redesign: now multithreaded. Needs cleaning up.