fixed bugs, refactoring