Second attempt, more efficient