Move this section out of handleEvent to avoid to long function