Correct the logic. Add comment, check formatting