Some improvement for #1505, but still wip.