Significant refactoring of instrument.c