implementation now matches wikipedia