Added more conditional logic