Refactoring + bug fixing