lots of code cleanup, bug fixes, and hacks for the "chord problem"