changed do whiles to try catches for more robust error handling