Proof-of-concept code. Very dirty so use with caution.