Adding private interface for easier unit testing.

Used TDD and added a bunch of unit tests. fleshed out matching logic.

Cleanup and refactoring.

Change-Id: I2b7d02a683aee0af934e61756a2ceec557a9eafd