Use utf8param($r, $key) instead of $r->param($key) throughout.