copied code over from cfg project. split into many files