Improved C solution