old_contents: import os
  import re
  
  from collections import OrderedDict
  from subprocess import Popen, PIPE
  
  LMOD_CMD = os.environ['LMOD_CMD']
  LMOD_SYSTEM_NAME = os.environ.get('LMOD_SYSTEM_NAME', '')
  
  def module(command, arguments=()):
      cmd = [LMOD_CMD, 'python', '--terse', command]
      cmd.extend(arguments)
  
      result = Popen(cmd, stdout=PIPE, stderr=PIPE)
      if command in ('load', 'unload', 'restore', 'save'):
          exec(result.stdout.read())
  
      return result.stderr.read().decode()
  
  def module_avail():
      string = module('avail')
      list_ = string.split()
      modules = []
      for i, entry in enumerate(list_):
          if entry.startswith('/') or (re.match("[A-Za-z\/]*", entry) and
                                       i != (len(list_) - 1) and
                                       list_[i+1].startswith(entry)):
              continue
          else:
              modules.append(entry)
      return modules
  
  def module_list():
      string = module('list').strip()
      if string != "No modules loaded":
          return string.split()
      return []
  
  def module_savelist(system=LMOD_SYSTEM_NAME):
      names = module('savelist').split()
      if system:
          suffix = '.{}'.format(system)
          n = len(suffix)
          names = [name[:-n] for name in names if name.endswith(suffix)]
      return names
  
new_contents: import os
  import re
  
  from collections import OrderedDict
  from subprocess import Popen, PIPE
  
  LMOD_CMD = os.environ['LMOD_CMD']
  LMOD_SYSTEM_NAME = os.environ.get('LMOD_SYSTEM_NAME', '')
  
  def module(command, arguments=()):
      cmd = [LMOD_CMD, 'python', '--terse', command]
      cmd.extend(arguments)
  
      result = Popen(cmd, stdout=PIPE, stderr=PIPE)
      if command in ('load', 'unload', 'restore', 'save'):
          exec(result.stdout.read())
  
      return result.stderr.read().decode()
  
  def module_avail():
      string = module('avail')
      modules = []
      for entry in string.split():
          if not (entry.startswith('/') or entry.endswith('/')):
              modules.append(entry)
      return modules
  
  def module_list():
      string = module('list').strip()
      if string != "No modules loaded":
          return string.split()
      return []
  
  def module_savelist(system=LMOD_SYSTEM_NAME):
      names = module('savelist').split()
      if system:
          suffix = '.{}'.format(system)
          n = len(suffix)
          names = [name[:-n] for name in names if name.endswith(suffix)]
      return names
  
subject: Simplify module_avail