better error handling and logging, simplified code

There's still a lot to do here with the logging,
but this is a step in the right direction.
We also need better error checking.