Made the function work propertly