working version, but non-ideal API