refactored php code