Fixed bugs and made refactoring