removed bugs, also refactoring