Fixed purge variable issue by creating variable file handles, code tidyup