split function up into other functions refactor to reduce the number of variables flying around.
split function up into other functions
refactor to reduce the number of variables flying around.