Revert "Revert "try catching main declaration earlier""

This reverts commit d7cb39c08101501338015139eb9c1f0277ede8a1.
Revert "try fixing declaration order again"

This reverts commit e9b26ad24c14d99e0fcade67746d7f42c29db8a4.
Revert "fix declaring global after function"

This reverts commit 876a21034fc50538358120ec0ec4cf86bae39913.