This is now complicated enough that I’d suggest adding a comment to explain what each bit is for and the context behind it.