Fixed bugs and tested