Refactor toFunction to asFunction