added more changes to make code more robust