Refactor some formatting code to make it shorter and simpler