Refactoring, fixed bugs