Rolling back my optimizations. Going back to the original code to make things work again but generates lots of errors in php log. More time should be spent on this refactoring.