foreach version of costrasters.R, still an error somewhere