Fix to tests and code cleanup