It's great that this is now extracted into reusable function, but I'm a bit scared by the super complicated function signature.

Could you perhaps define a record to store some of the arguments? (Those where it logically makes sense to group them into a separate data structure?)