more or less working magma code