Lots of speed improvements when listing packages - don't use a tempfile, grep before sort/uniq, and most importantly on list packages with the prefix in them. Also, add quoting around variable names and simplify boolean usage with true/false.