rewrite code a bit