Fixed bugs and condensed code