Replace loop with map