fixed the r script and improved the output