Added for loop and refactored