refactor + fix spark error on logout - see issue 392