added similar implementation as at line 145