total rewrite the logic