clean up code, fix bug in multiple subs