I've fixed some bugs in this code