Part of #686
With this change, all definitions and functionality for a given metric are in a single place only instead of being distributed all over the file. This makes it easier to inspect the code for correctness and adding/changing metrics.