Separate listener functionality from main
Refactor listener into more functions