Started work on legend. Moved some math helper functions from individual modules to wm_utils.py.