Major overhaul of gui code