Initial bug-free version
merge with older codes