Removed dead code and fixed bug