Added all the logic to make it work