Tweaking function to work properly