Add sorting to avoid deadlocks.
Replace exception with logging.