fixed loop to work with variable