enhance and optimize logging and error handling