Refactor code to be able to test properly