made nesting function more advanced