Completed cleanup function that actually works now. Adds new key that defines the N margin for cleanup