tidied up main loop with pointer references