Improved overall safety.

Threw smart pointers everywhere and fixed some very stupid errors.