Added pinning function, fixed some bugs.