Added error handling to the time function