Code clean up

Squashed a couple bugs. See commits for details.