Needs thorough review. Moved from fusedav.c but with significant changes