Some cleanup in sys.c, getting rid of duplicated code