huge code refactoring

… and dead code removal