Comparative studies of O2 and N2 in pure, mixed and layered CO ices