fixing bugs and refactoring stuff