added a listener class and fix null pointer errors