refactored code and boost speed a little bit, embedded into pipeline