Converted to PDO
Minor optimiztions