Slightly better solution for q5