Move code which does some basic checks into separate CMake file. Some additional reordering of the code is needed.