Programmed function to calculate 'firsts' set. It appears to work, but it nees more testing.