Rather than wrap the functionality of this code in "if ( empty( $var ) )", I just check to see if its set and return early.  Many of the following changes come from removing the previous code indentation.