cleanup code
use bool type where appropriate
replace while by for loop