Refactor visualization code