Extract some functions Changes made by Krasi Zarev (krasizarev@msn.com).