improve mem usage and intorduce client loop