Move to PHP5.4, fix bugs and add tests

Not ready to merge, 2 tests to fix.
I will update the algorithm soon, in a different PR.