Refactored code to seperate methods from UI