Refactored gui code