Improve some error cases

Reduce repeated logic