#332 refactoring the code to make it opensourceable.