Commented and fixed logic