refactored and moved variables all levels