Integrate metrics support and refactor the code.