Refactored metric_handler

Split out into simpler classes.
Add tests