more error handling and logging added to the mix