Changed error handling again because it clearly doesn't work in a function