edited logic a bit