fix error in detect and wrap in a function