Refactor code to have only one event handler for start and end markers