Working solution for #49, needs verification.