The finalised version. Added logout mechanism