changed loops to c99