Refactored optimize to avoid nil pointer dereference + improved reusability of the code
Refactored optimize to avoid nil pointer dereference + improved reusability of the code

moul/dockerpatch,moul/dockerpatch,moul/dockerpatch