complete rewrite of php, minor style changes