Fix a failing test and slightly optimize some code