Found a more optimal solution using the map function