re-factorization to fulfill requirements of OpenCV API