Added missing test and improved logic