Code cleanup and commenting.
Add BSD 3 clause licence