Refactor tests to make them more stable