fixed logic as @tonysyu pointed out