factoring and bug fix.