Added Doc, references

Needed to clean up the code a bit, so added documentation, and a bit of
formatting, because we like clean code and comments where appropriate.