Minimized code redundancy and fixed some bugs