Fixed horrible bug; somewhat subtle memory leaks.