less hacky thanks to studying @wcline's branch