Major re-organizing of main.R

Making R code compatible with buck cluster.