with this code it really needs tests to ensure it works ..