refactor to use assert instead of if