Adding testing and fixing the function