Further refactorization plus dead code removal and some commenting