Added logout function; and more reorganising