Refactor to_int error handling code