Fixed memory issues.

The code was immensely bloated. Many more libraries were included
than were actually needed. Thinned out to only what is necessary,
and fixed up the memory stuff.