First part of major rewrite. Requirements changed.