variable logic fix and cleanup