Fixed a few corner case bugs