Fix if statements (thanks to Anne Mulhern), improve error handling