Changed main logic a bit