legality checking loop in here

see the changes. pretty simple stuff. Might be worth to put the legality loop in a separate function later however