Not really a big problem but: #define MIN(a,b) (((a)<(b))?(a):(b))
should be used here.