Yes, a bit of duplication here. I wasn't sure this code should be merged at all as it's just a naive way to fix a problem (see PR message), so I didn't bother about quality to much.
I'll update it in a minute.