Fixes #871; Props @rulatir for initial patch