Pull generally useful routines out into tools.c