Improving apiEvent, reordering code