Rework CSP and add tests

Fixes #23.