this needs rewritten.