Rewritten version to optimize requests and use of PHP5 OOP