Extracted If condition in line 35

also some bug fixes