pop function modified to ret value