Rewrite to handle edge cases.