Fixed failing tests.

Also refactored.