huge ugly freaking rewrite