Added various fixes and edge cases.