a mess, but seems like it's working...