++ rewrite to Kotlin