Add documentation, some fail-safe logic