Add various event listeners and error protection.