bug fix and condensed code