Rework makefiles. Add proper dependency checking.