Moved optimisation code into core.

Removed most System.out, System.err and replaced with logging... still a few more to go

Removed all inappropriate System.exit calls and replaced with exceptions.