and modernize the script to take advantage of SUB_FILES. Also simplify it a bit without losing any functionality.