fixed my messy untested code.