Removed debug and fixed logic