Moved function definitions outside conditional statements; removed unnecessary variable