Added guard clauses to while-loops