Move to PHP5.4, fix bugs and add tests

Not ready to merge, 2 tests to fix.