Some code is ported from NFlabs Peloton (www.nflabs.com)