Refactor old getHandle.php code