Making the code a bit more robust