refactor code for R and AHK