Reduce some code nesting