Moved pixel copying code to separate functions