Refactored similarity.py and files which depend on it.