improve apply() function template