This was ported 1:1 from the code we are currently using. I'm not against improving this part but I think that we shouldn't try to stick everything into this PR. We should probably create a ticket improving this behavior though.