refactorizing + solving bugs