Further cleanup of code and fixing of unit tests