Rewrote with Kotlin.