First phase of refactor complete

Code is now modular and has been improved and fixed in some areas.