Removed redundant code and fixed bug