Fixed some issues with corner cases