What if "$D" contains special characters? Split out the logic into a function:

    Handler() { kill -9 "$B1PID" "$B2PID"; rm -rf "$D"; }
    trap Handler EXIT