cleaned up the functions using global variables