Added function to wrap errors in, logging where they occur.