added more robust code