changed to use $opts, instead of just a $debug flag. Also added the first parameter "limit"