Fix logout. Some code cleanup