Code refactoring

Also added a logout button and various bugfixes