trying to fix the bug, not fully tested