rewrite buildByCoutours function