(Partial) rewrite of fig7 code