finished refactoring Terry's code to the point it works. would love to make it prettier though...