Code refactored

Added namespaces.
Added use singleton to avoid use of global.
Adapted the code to PSR-1.