Moved some setup code to external functions to enhance code readability