Most of this code is prepared by @iremsalgar. Thank you for allowing me to use it.