Code refactoring and getting the events to work for appended markup