Refactor the makefile using templates