Some simplificaiton and refactoring