Merge makefile improvements from afb/makefile into master

@kboyd, I fixed some issues and made some improvements to the makefile. What do you think? Ok to merge into master? (After this the plan is to merge master into dev/afbarnard and then bring all those changes into master which gives us a working main. Yay!)