restructure Makefiles and fix Linux and Mac builds