Fix 478 and some other refactoring