Refactor of devices.php logic to be more direct