I think this needs to be moved into the below if statement.  Getting a "cannot call method toString of null" error when the createBadgeInstance succeeds.