added comments and changed condition of second if