refactored and fixed some shared variable issues