Replace assert with if/else. Note that the assert seems to be noticebly slower than using an if/else.