Resolved issue with variable inside loop