A method for subtracting foregrounds from multifrequency CMB sky maps