Adds some doc commentaries and prettifies the code... slightly.