added state machine so now it will work with up to five instructions