copied stuff over from gem