Refactored all php code