Fixed unit tests and fixed some potential problems.