php code refactored