rearranged the argument checks at the top of the function to avoid dependencies on variables that are not yet defined. also updated some syntax