added getfirst and getlast, and fixed some bugs