small fixes, big refactorings.