Nicknames

Hi there.  A friend and I were recently wondering about how certain English diminutives, or nicknames, were dreamed up. For example, A short form of Margaret is ....um....Peg?  William became Bill....where the heck did that "B" come from?  Edward becomes ....Ted...or, even worse, Ned!  What's with the random placement of new letters at the beginning of names like those?  And let's not forget little Dick, formerly Richard.  So strange.  In general, does anyone have any insight, theories, or facts on this?