Major revision with the main file now reduced considerably by moving functions to outdie modules. STILL NEEDING DEBUGGED