refactored and improved functions reading/writing boxed values