Merge pull request #1 from dostapenko/master

legality checking loop in here
Dmitri, here's the stuff I described.
Debugging stuff
Debugging stuff
fix for out of idnex crash