added pack and unpack and done some more refactoring