Added error catching and logging