this is a working version... but needs some minor mods for error checking etc..