modernize code

broke code down and minimized the number of globals