organized horrific code; made some efficiency improvements