Complted initial refactoring for version 0.4