rewrite using primitive functions