Fix makefile
Refactor some code to put responsibilities in the right places