Fixed a bunch of edge cases.