Refactor and make the test more robust.