Multiple bugfixes due to PDO migration
Cleaned up global variable usage
Tree fix