condensed some code. Made use of global variables.