Add support for calculating labels and fix a bunch of bugs