--- Makefile
+++ Makefile
@@ -1,75 +1,37 @@
-CC=gcc
-CCFLAGS=-O2 -Wall
-EXTRACCFLAGS=-s -lezV24
-EXPATCCFLAGS=-lexpat
-CFILES=put.c get.c cobex_core.c ls.c cobex_tools.c cobex_serial.c rm.c mkdir.c
-HFILES=cobex_defs.h cobex_core.h capabilities.h cobex_tools.h cobex_serial.h
-O2FILES=cobex_core.o cobex_tools.o cobex_serial.o
-PUTO=put.o
-GETO=get.o
-LSO=ls.o
-RMO=rm.o
-MKDIRO=mkdir.o
-CAPAO=capability.o
-COBEXPUT=cobex_put
-COBEXGET=cobex_get
-COBEXLS=cobex_ls
-COBEXRM=cobex_rm
-COBEXCAPA=cobex_capa
-COBEXMKDIR=cobex_mkdir
+CC ?= gcc
+CFLAGS ?= -O2
+CFLAGS += -Wall
+LDLIBS += -lezV24
+
+PROGS = cobex_put cobex_get cobex_ls cobex_rm cobex_mkdir
+CFILES = $(wildcard *.c)
+HFILES = $(wildcard *.h)
+O2FILES = cobex_core.o cobex_tools.o cobex_serial.o
+
 EXTRAFILES = README Changelog COPYING Makefile cobex_put.pl Things_to_know_T310.txt Hacking_pl2303_driver.txt
 VERSION = 0.2.13
 PROJNAME = cobex-$(VERSION)
 
-all: $(COBEXPUT) $(COBEXGET) $(COBEXLS) $(COBEXRM) $(COBEXMKDIR)
+all: $(PROGS)
 
 model:
 	cd modeldb && make all 
 
-$(COBEXPUT): $(O2FILES) $(PUTO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) -o $(COBEXPUT) $(PUTO) $(O2FILES)
-
-$(COBEXGET): $(O2FILES) $(GETO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) -o $(COBEXGET) $(GETO) $(O2FILES)
-
-$(COBEXRM): $(O2FILES) $(RMO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) -o $(COBEXRM) $(RMO) $(O2FILES)
-
-$(COBEXLS): $(O2FILES) $(LSO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) $(EXPATCCFLAGS) -o $(COBEXLS) $(LSO) $(O2FILES)
-
-$(COBEXCAPA): $(O2FILES) $(CAPAO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) -o $(COBEXCAPA) $(CAPAO) $(O2FILES)
-
-$(COBEXMKDIR): $(O2FILES) $(MKDIRO)
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) -o $(COBEXMKDIR) $(MKDIRO) $(O2FILES)
-
-
-test2: $(O2FILES) test2.o
-	$(CC) $(CCFLAGS) $(EXTRACCFLAGS) $(EXPATCCFLAGS) -o test2 test2.o $(O2FILES)
-
-.c.o:
-	$(CC) $(CCFLAGS) $(LOCKSERIAL) -c $<
-
-$(PUTO): put.c $(O2FILES)
-
-$(GETO): get.c $(O2FILES)
-
-$(LSO): ls.c $(O2FILES)
-
-$(RMO): rm.c $(O2FILES)
-
-test2.o: test2.c $(O2FILES)
-
-cobex_core.o: cobex_core.c cobex_core.h cobex_defs.h
-
-cobex_tools.o: cobex_tools.c cobex_tools.h cobex_defs.h cobex_core.c cobex_core.h
-
-cobex_serial.o: cobex_serial.c cobex_serial.h cobex_core.c cobex_core.h
+cobex_put:   $(O2FILES)
+cobex_get:   $(O2FILES)
+cobex_ls:    $(O2FILES)
+cobex_rm:    $(O2FILES)
+cobex_mkdir: $(O2FILES)
+cobex_capa:  $(O2FILES)
+
+cobex_ls: LDLIBS += -lexpat
+
+-include .depend
+.depend: $(CFILES) $(HFILES)
+	$(CC) -MM $^ > .depend
 
 clean:
-	rm -f *.o
-	cd modeldb && make clean
+	rm -f *.o $(PROGS)
 
 printfiles: $(CFILES) $(HFILES)
 	@echo $?
@@ -88,3 +50,5 @@
 
 dist-contrib :
 	cd contrib && make dist VERSION=$(VERSION)
+
+.PHONY: all clean dist dist2 dist-modeldb dist-contrib model printfiles
