Refactoring first stage + issue fixing