Add exception handling. Change to use for loop