creation of kernel matrix
restructured code into subfunctions