Making the logic clearer in second function

Thanks to @ultimatehurl