made code more robust