Error/Debug fixes and Performance fixes

Error types defined for all errors and methods return errors on exit
for the GUI
Pass by reference on large data structures into methods.