copy from https://github.com/TjaLfE/cps.nextgen/blob/master/main.py

I have raw copied the changes that TjaLfE did after the changes in nxtgn's design