Fix and refactor tests