Full re-write, once again.

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>