Fixed a few interesting bugs