Some refinements, refactoring and cleanup