fixed bugs, small refactoring