A couple of quick fixes thanks to @cursedcoder, still need to improve testing.