Complete rewrite. No longer needs inp/pes file