fixed a bug, now it's working. Added some comments