Rewrite weird logic