Fix some bugs in that weird part of code