Fix bug and remove dead code