fix bug descibe on issue#1 and refocator code