Removed some dead code and simplified some other ocde