Put convert code in separate function

Prevent "too many open files" error.