added delay optimization fixes by Peter