Removed some redundant code and fixed bugs