Fixed bugs and removed uneeded code