Better error checking / exception handling