Refactor some GUI code