fix many bugs

Fixed https://forge.indepnet.net/issues/4986
Fixed https://forge.indepnet.net/issues/5020