split out bag code; fixed count bug for search