Restructured and refactored programm. now output into a csv file