Fixed bugs and added PHPUnit test (not yet working)

Signed-off-by: Nick Brook <nick.brook@ratemyplacement.co.uk>