Applying patch. Fixed some bugs.