Code rewrite with added metrics