this does work but is a bit dodgy. needs some rearchitecturing