Fixes for the double casting error, plus some more cleanup and documentation.

Signed-off-by: Jason Kuster <unexpectedapocalypse@gmail.com>