Handled some corner cases and added tests