Add similarity table. Fix a few bugs.