Sorry @luisuribe, I'm impatient and I needed working code to be able to carry on what I'm working on so this pull can replace your own, if you want to, or just use it as a guide to update the existing pull.
I'm still in the process of testing; I don't think this is fully working, actually, so there may be more fixes incoming.
Also migrate to modern (PHP 5.4+) array syntax.
Sorry @luisuribe, I'm impatient and I needed working code to be able to carry on what I'm working on so this pull can replace your own, if you want to, or just use it as a guide to update the existing pull.
I'm still in the process of testing; I don't think this is fully working, actually, so there may be more fixes incoming.