Added checks in a separate file (and fixed some bugs).