Move tools code to where it is used