cleaned up the c code somewhat