simplification, tiding and bug fixing