Move main conversion code to separate function