unfixed and updated the logic