remove redundant code and fix few bugs