really duct-tape code, but it works