revamped reverse() function, fixed many bugs