updated and reworked some of Dr. Wisor's code