final version with bug fix