Modified logic to make it work?