Modified from https://github.com/ethanhart/pypacc