Better first pass. Passes unit tests.