Fix for... in loops to avoid looping through the prototype

Added hasOwnProperty() checking to the for... in loops in use, otherwise the loop would loop through all properties of all objects in the prototype tree of the target, even Object.prototype. This can also cause weird bugs when something extends Object.prototype.