Some code for handling events in viewer