Functional and secure code. Fixes #4. Fixes #2. Fixes #1.