laid framework for detect_pkgs, cleaned up code