Fix term names to use IDs rather than names

Commit 567eeb85f85d19e11011e8551882790af7c29fab fixed term names to use
the IDs of the images, since two different images can have the same name
but different id. This commit reintroduces this change, along with minor
style modifications.