Basic, untested code. Could probably be optimised

Should probably test this when I can be bothered to get a LAMP stack
installed in Windows...