10adapted from http://github.com/SnippyHolloW/bwrepdump/blob/master/BWRepDump.cpp\#L1773 and http://github.com/SnippyHolloW/bwrepdump/blob/master/BWRepDump.cpp\#L1147