Updated loop method to prevent nulls