fixed bugs, refactored