Fix variable scopes for better capsulization