Moved code to work better in testing.
added more tests.