modified into general form (NOT TESTED!)