Fixing bug and unused code