refactor figure generation