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?