Added better error handling

This also prevents a crash