Began work on fixing code so this will run as a library properly.