did some refactoring for opimization