implemented joy's code