refactored it here ce8d5601a3ac2bd74dd634bbbcc4284d3f0eafdc