SQL tweaks: table better suited to actual data.
Cleanup.

No useless variables ;
Better conformance to PSR-1 and 2 standards.