code cleanup and listener fix