Refactored code for making figures