Version 2. Logic mostly complete.