Add better error handling (prevent crash on bad input)