More robust code and error handling