Refactoring + bug fixes + improvements