Rewrite code using q