Some refatorization and coding style improvements