code adapted but crashes