Misc cleanup with variables and moved some things into functions