fix a build issue + a bit of refactoring