Refactored val copying code and added comments