Move main code to a subroutine, prevents current/next/etc being global