Handled function boundaries.
Moved null-guards to appropriate places, added helpers.