Consolidate code and fix unit tests and test scripts