Completed code but I might have caused an infinite loop