codes by @ivan