Performed a large re-factor so code is more testable. So test remain to be fixed.