changed from cpp to c; optimized for router