Rewrite code for clarity/potential bug