Added error logging and commenting