refactor code to make event handlers and drawText functions