Unit Tests, several bug fixes. Code cleanup