Refactored a little - got rid of global variable