Code restructured to separate generic functions and driver functions