moving more code into functions...not really satisfied with it yet, but regression test it