code sync with original FreeUI and some minor fix