add attach instrument, and cleaned up code.