Use a copy of the metrics object instead of the original, fix global leaks and some refactors.