some comments and duplicate code removed