Fixed tests and performed some refactoring.