Fix logging. Refactor tests