Rolling back my optimizations. Going back to the original code to make t...

...hings work again but generates lots of errors in php log. More time should be spent on this refactoring.