Stole some of Andreas logout-changes