minor code improvements to ensure no null pointer exceptions and slight improvement in boolean branching to avoid double expression testing