Rewrite to make it work more consistently