Refactored the MIDI code.