Small refactor
Greatly improved error handling replacing all calls to exit() with return values. Also added documentation to every function