Added comments and refactored the functions

Added extensive comments.
Refactored the functions to remove the double negative condition from
the if...else conditions.
Refactored to have only 1 return. Not essential but simpler in such
small functions.