collect malloc logic into its own method