Updated version that works when refreshing the view