maintenance

put code into functions
moved code around