Refactoring + small fixes.