Fix and remove redundant code