Added a highly robust logout function