a lot of tweaking to get things working