refactor cosine similarity