Makefile fixes (rebase of #1366)

Thanks a lot for these fixes
Closes #1366