2nd refactor function logged in