changed functions so no variables have global scope