lost of probably unnecessary casting and converting, but it works.